/*
 *接口编写处...
 *如果接口需要做Action的权限验证，请在Action上使用属性
 *如: [ApiActionPermission("InventoryModel",Enums.ActionPermissionOptions.Search)]
 */
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Http;
using VOL.Entity.DomainModels;
using VOL.demo.IServices;

namespace VOL.demo.Controllers
{
    public partial class InventoryModelController
    {
        private readonly IInventoryModelService _service;//访问业务代码
        private readonly IHttpContextAccessor _httpContextAccessor;

        [ActivatorUtilitiesConstructor]
        public InventoryModelController(
            IInventoryModelService service,
            IHttpContextAccessor httpContextAccessor
        )
        : base(service)
        {
            _service = service;
            _httpContextAccessor = httpContextAccessor;
        }

        /// <summary>
        /// 根据货品ID获取库存信息
        /// </summary>
        /// <param name="goodsId">货品ID</param>
        /// <returns></returns>
        [HttpGet("GetInventoryByGoodsId")]
        public IActionResult GetInventoryByGoodsId(long goodsId)
        {
            try
            {
                var result = _service.GetInventoryByGoodsId(goodsId);
                return Json(new { success = true, data = result });
            }
            catch (Exception ex)
            {
                return Json(new { success = false, message = ex.Message });
            }
        }

        /// <summary>
        /// 根据仓库ID获取库存信息
        /// </summary>
        /// <param name="warehouseId">仓库ID</param>
        /// <returns></returns>
        [HttpGet("GetInventoryByWarehouseId")]
        public IActionResult GetInventoryByWarehouseId(long warehouseId)
        {
            try
            {
                var result = _service.GetInventoryByWarehouseId(warehouseId);
                return Json(new { success = true, data = result });
            }
            catch (Exception ex)
            {
                return Json(new { success = false, message = ex.Message });
            }
        }

        /// <summary>
        /// 批量设置库存上下限
        /// </summary>
        /// <param name="request">批量设置请求</param>
        /// <returns></returns>
        [HttpPost("BatchSetInventoryLimits")]
        public IActionResult BatchSetInventoryLimits([FromBody] BatchSetInventoryLimitsRequest request)
        {
            try
            {
                var result = _service.BatchSetInventoryLimits(request.InventoryIds, request.UpperLimit, request.LowerLimit);
                if (result.Status)
                {
                    return Json(new { success = true, message = result.Message });
                }
                else
                {
                    return Json(new { success = false, message = result.Message });
                }
            }
            catch (Exception ex)
            {
                return Json(new { success = false, message = ex.Message });
            }
        }

        /// <summary>
        /// 保存库存信息
        /// </summary>
        /// <param name="inventory">库存信息</param>
        /// <returns></returns>
        [HttpPost("SaveInventory")]
        public IActionResult SaveInventory([FromBody] InventoryModel inventory)
        {
            try
            {
                var result = _service.SaveInventory(inventory);
                if (result.Status)
                {
                    return Json(new { success = true, message = result.Message, data = result.Data });
                }
                else
                {
                    return Json(new { success = false, message = result.Message });
                }
            }
            catch (Exception ex)
            {
                return Json(new { success = false, message = ex.Message });
            }
        }
    }

    /// <summary>
    /// 批量设置库存上下限请求模型
    /// </summary>
    public class BatchSetInventoryLimitsRequest
    {
        public List<long> InventoryIds { get; set; }
        public int? UpperLimit { get; set; }
        public int? LowerLimit { get; set; }
    }
}
